﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using MySql.Data.MySqlClient;

using HolisticWare.VoIP.Getafisk.DLL_01DA_netfx35.any_any_any.FreePBX;

namespace HolisticWare.VoIP.Getafisk.DLL_01DA_netfx35.any_any_any
{
	public 
		class 
		DataBase
	{
		MySqlConnection mysql_connection;

		string ConnectionString =
			// "Persist Security Info=True;server=%IP%;User Id=root;Pwd=hwtest;database=asterisk"
			Properties.Settings.Default.ConnectionString.Replace("%IP%","192.168.11.221")
			;

		public
			string
			DebugStatus
			;

		public
			DataBase
			(
			)
		{
			mysql_connection = new MySqlConnection(ConnectionString);

			//......................................................................
			//string msg = string.Empty;
			StringBuilder sb = new StringBuilder();
			sb.Append("MySQL: ");
			sb.Append(Environment.NewLine);
			sb.Append("connection string=");
			sb.Append(ConnectionString);
			sb.Append(Environment.NewLine);
			//sb.Append("");
			//sb.ToString();
			//return msg;
			//......................................................................

			try
			{
				mysql_connection.Open();
				sb.Append("opened version=");
				sb.Append(mysql_connection.ServerVersion);
				sb.Append(Environment.NewLine);
			}
			catch (MySqlException e)
			{
				sb.Append("error =");
				sb.Append(e.Message);
				sb.Append(Environment.NewLine);
			}
			DebugStatus += sb.ToString();

			_FreePBXModel = new FreePBX.FreePBX(mysql_connection);

			return;
		}

		# region ---- Property FreePBX FreePBXModel --------------------
		private FreePBX.FreePBX _FreePBXModel;

		public FreePBX.FreePBX FreePBXModel
		{
			get { return _FreePBXModel; }
			set
			{
				if (value != _FreePBXModel)
				{
					// lock(_FreePBXModel) // MultiThread safe
					{
						_FreePBXModel = value;
						if (null != FreePBXModelChanged)
							FreePBXModelChanged(this, new EventArgs());
					}
				}
			}
		}

		public event EventHandler FreePBXModelChanged;
		# endregion ---- FreePBX Property.FreePBXModel --------------------
	
		public
			List<Sip>
			ExtensionsSIP
			(
			)
		{
			List<Sip> extensions_sip = default(List<Sip>);
			try
			{
				extensions_sip =
						(
						  from es in _FreePBXModel.Sip
						  select es
						 ).ToList<Sip>();
			}
			catch (System.Exception exc)
			{
				string msg = exc.ToString();
			}

			return extensions_sip;
		}

		public
			List<FreePBX.IAx>
			ExtensionsIAX
			(
			)
		{
			List<FreePBX.IAx> extensions_iax =
					(
					  from FreePBX.IAx ei in _FreePBXModel.IAx
					  select ei
					 ).ToList<FreePBX.IAx>();

			return extensions_iax;
		}

		public
			List<FreePBX.Trunks>
			Trunks
			(
			)
		{
			List<FreePBX.Trunks> trunks =
					(
					  from FreePBX.Trunks t in _FreePBXModel.Trunks
					  select t
					 ).ToList<FreePBX.Trunks>();

			return trunks;
		}

		public
			List<FreePBX.Incoming>
			RoutesInbound
			(
			)
		{
			List<FreePBX.Incoming> trunks =
					(
					  from FreePBX.Incoming i in _FreePBXModel.Incoming
					  select i
					 ).ToList<FreePBX.Incoming>();

			return trunks;
		}

	}
}
